//
//  WCProjectGroup.m
//  WabbitCode
//
//  Created by William Towe on 1/22/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCProjectGroup.h"
#import "WCProject.h"
#import "WCTextFile.h"
#import "NSTreeNode+WCExtensions.h"
#import "WCAppController.h"


static NSFileManager *fm = nil;
static NSWorkspace *sw = nil;

@implementation WCProjectGroup
#pragma mark *** Subclass Overrides ***
+ (void)initialize {
	fm = [NSFileManager defaultManager];
	sw = [NSWorkspace sharedWorkspace];
}

- (NSString *)name {
	return [[self project] projectName];
}

- (NSImage *)icon {
	return [NSImage imageNamed:@"project"];
}

- (BOOL)canRenameNode {
	return NO;
}

- (NSString *)nodeToolTip {
	return nil;
}

- (NSString *)filePath {
	return @"";
}

- (NSString *)absoluteFilePath {
	return [[[self project] fileURL] path];
}

- (NSString *)folderPath {
	return [[self absoluteFilePath] stringByDeletingLastPathComponent];
}

- (BOOL)canDeleteNode {
	return NO;
}

- (BOOL)canGroupNode {
	return NO;
}

- (BOOL)canUngroupNode {
	return NO;
}

- (BOOL)isDragable {
	return NO;
}

- (BOOL)isRootLevelGroup {
	return YES;
}

- (BOOL)canGetInfo {
	return NO;
}

#pragma mark *** Public Methods ***
- (void)removeAllErrorsAndWarnings; {
	for (WCTextFile *file in [self descendantLeafNodes]) {
		if ([file isKindOfClass:[WCTextFile class]]) {
			[file removeAllFileErrors];
			[file removeAllFileWarnings];
		}
	}
}

- (WCFile *)fileWithFilePath:(NSString *)filePath; {
	for (WCFile *file in [self descendantLeafNodes]) {
		if ([[file absoluteFilePath] isEqualToString:filePath])
			return file;
	}
	return nil;
}

- (WCFile *)fileWithFileName:(NSString *)fileName; {
	for (WCFile *file in [self descendantLeafNodes]) {
		if ([[file name] isEqualToString:fileName])
			return file;
	}
	return nil;
}
#pragma mark Accessors
@dynamic project;
- (WCProject *)project {
	return _project;
}
- (void)setProject:(WCProject *)value {
	if (_project == value)
		return;
	
	_project = value;
	
	[[self descendantNodes] makeObjectsPerformSelector:@selector(setProject:) withObject:_project];
}

@dynamic projectFolderPath;
- (NSString *)projectFolderPath {
	return [[self project] projectFolderPath];
}

@dynamic projectFilePaths;
- (NSSet *)projectFilePaths {
	NSSet *paths = [[NSSet alloc] initWithArray:[[[self descendantLeafNodes] valueForKey:@"absoluteFilePath"] arrayByAddingObject:[self absoluteFilePath]]];
	return [paths autorelease];
}

@dynamic unsavedTextFiles;
- (NSArray *)unsavedTextFiles {
	NSMutableArray *files = [NSMutableArray array];
	
	for (WCTextFile *file in [self descendantLeafNodes]) {
		if ([file isKindOfClass:[WCTextFile class]] &&
			[file isEdited]) {
			[files addObject:file];
		}
	}
	return [[files copy] autorelease];
}

@dynamic textFiles;
- (NSArray *)textFiles {
	NSMutableArray *files = [NSMutableArray array];
	
	for (id file in [self descendantLeafNodes]) {
		if ([file isKindOfClass:[WCTextFile class]])
			[files addObject:file];
	}
	return [[files copy] autorelease];
}
#pragma mark Creation
+ (id)projectGroup; {
	return [super treeNodeWithNodeName:nil];
}
@end
